﻿using System;
using System.Xml;
using System.IO;

namespace Server
{
    public class Account
    {
        // Data Members
        string username;
        string password;
        string email;
        bool newCharacter = true;

        // Member Properties
        public string UserName
        {
            get { return username; }
            set { username = value; }
        }
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        public string Email
        {
            get { return email; }
            set { email = value; }
        }
        public bool NewCharacter
        {
            get { return newCharacter; }
            set { newCharacter = value; }
        }

        // Constructor
        public Account() { }

        // Methods
        public void ToXML(string File)
        {
            XmlTextWriter XWriter = new XmlTextWriter(File, null);
            XWriter.Formatting = Formatting.Indented;

            XWriter.WriteStartDocument();

            // Info Element
            XWriter.WriteStartElement("Info");
            {
                XWriter.WriteStartElement("Name");
                XWriter.WriteValue(UserName);
                XWriter.WriteEndElement();

                XWriter.WriteStartElement("Password");
                XWriter.WriteValue(Password);
                XWriter.WriteEndElement();

                XWriter.WriteStartElement("Email");
                XWriter.WriteValue(Email);
                XWriter.WriteEndElement();

                XWriter.WriteStartElement("NewCharacter");
                XWriter.WriteValue(NewCharacter);
                XWriter.WriteEndElement();
            }
            XWriter.WriteEndElement();

            XWriter.WriteEndDocument();
            XWriter.Close();
        }
        public void FromXML(string File)
        {
            XmlTextReader XReader = new XmlTextReader(File);

            while (XReader.Read())
            {
                switch (XReader.NodeType)
                {
                    case XmlNodeType.Element:
                        {
                            switch (XReader.Name)
                            {
                                case "Name":
                                    UserName = XReader.ReadString();
                                    break;

                                case "Password":
                                    Password = XReader.ReadString();
                                    break;

                                case "Email":
                                    Email = XReader.ReadString();
                                    break;

                                case "NewCharacter":
                                    NewCharacter = XReader.ReadContentAsBoolean();
                                    break;
                            }
                        }
                        break;
                }
            }

            XReader.Close();
        }
        public void ToBinary(string File)
        {
            FileStream Fstream = new FileStream(File, FileMode.Create);
            BinaryWriter Writer = new BinaryWriter(Fstream);
            {
                // Write out Account
                Writer.Write(UserName);
                Writer.Write(Password);
                Writer.Write(Email);
                Writer.Write(NewCharacter);
            }
            Writer.Close();
            Fstream.Close();
        }
        public void FromBinary(string File)
        {
            FileStream Fstream = new FileStream(File, FileMode.Open);
            BinaryReader Reader = new BinaryReader(Fstream);
            {
                UserName = Reader.ReadString();
                Password = Reader.ReadString();
                Email = Reader.ReadString();
                NewCharacter = Reader.ReadBoolean();
            }
            Reader.Close();
            Fstream.Close();
        }
    }
}
